﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;


namespace mojoPortal.Web.RS.SBM
{
    public partial class RSViewCalls : RSBasePage
    {
        private int _companyid;

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                InitializeReadyServicePage();
                SetRSPageHeader("Service Calls");
                RSUserIdentity userIdentity = GetRSUserIdentity();
                _companyid = userIdentity.RSCompanyId;

                LoadLocationDropdownList();
                LoadCallStatusDropdownList();
                LoadCallTypeDropdownList();

                LoadCallGridView();
            }
            
        }
        protected void btnAddNewServiceCall_Click(object sender, EventArgs e)
        {
            Response.Redirect("RSAddNew.aspx");
        }
        protected void gvCallList_Select(object sender, CommandEventArgs e)
        {
            int callid = Convert.ToInt32(e.CommandArgument.ToString());
            Response.Redirect("RSCallDetail.aspx?callid=" + callid);
        }

        private void LoadCallStatusDropdownList()
        {
            SqlCommand myCommand;
            SqlDataReader myDataReader;
            String sqlSELECT = "select statusid, StatusName from rs_tblStatus where companyid=@companyid and category='CALL_CAT' order by StatusName";
            SqlParameter param = new SqlParameter();
            SqlConnection myConnection;

            myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
            myCommand = new SqlCommand(sqlSELECT, myConnection);
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@companyid";
            param.Value = _companyid;
            myCommand.Parameters.Add(param);
            myConnection.Open();
            myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
            ddlCallStatus.Items.Clear();
            ddlCallStatus.DataSource = myDataReader;
            ddlCallStatus.DataTextField = "StatusName";
            ddlCallStatus.DataValueField = "StatusId";
            ddlCallStatus.DataBind();
            ddlCallStatus.Items.Insert(0, new ListItem("", ""));
            myConnection.Close();
        }

        private void LoadLocationDropdownList()
        {
            SqlCommand myCommand;
            SqlDataReader myDataReader;
            String sqlSELECT = "select * from rs_vwLocationWithCall where companyid=@companyid order by 1";
            SqlParameter param = new SqlParameter();
            SqlConnection myConnection;

            myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
            myCommand = new SqlCommand(sqlSELECT, myConnection);
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@companyid";
            param.Value = _companyid;
            myCommand.Parameters.Add(param);
            myConnection.Open();
            myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
            ddlLocation.Items.Clear();
            ddlLocation.DataSource = myDataReader;
            ddlLocation.DataTextField = "Location";
            ddlLocation.DataValueField = "Location";
            ddlLocation.DataBind();
            ddlLocation.Items.Insert(0, new ListItem("", ""));
            myConnection.Close();
        }

        private void LoadCallTypeDropdownList()
        {
            SqlParameter param = new SqlParameter();
            SqlCommand myCommand;
            SqlDataReader myDataReader;
            String sqlSELECT = "select typeid, typename from rs_tblType where category='CALL_CAT' and companyid=@companyid order by typename";
            SqlConnection myConnection;

            myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
            myCommand = new SqlCommand(sqlSELECT, myConnection);
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@companyid";
            param.Value = _companyid;
            myCommand.Parameters.Add(param);
            myConnection.Open();
            myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
            ddlCallType.Items.Clear();
            ddlCallType.DataSource = myDataReader;
            ddlCallType.DataTextField = "TypeName";
            ddlCallType.DataValueField = "TypeId";
            ddlCallType.DataBind();
            ddlCallType.Items.Insert(0, new ListItem("", ""));
            myConnection.Close();
        }

        private void LoadCallGridView()
        {
            SqlCommand myCommand;
            String sqlSELECT = "select CallId, CallDt, Subject, Description, Response, CompanyId, CallTypeId, CallType, City, State, cast(LocationId as varchar(6)) + ' - ' + Location as Address , LocationId, Zip, StatusId, Status from rs_vwCall";
            String sqlWHERE = " where companyid=@companyid";

            List<string> paramlist = new List<string>();
            paramlist.Add("@companyid");

            if (txtCallID.Text != "")
            {
                sqlWHERE = sqlWHERE + " and callid=@callid";
                paramlist.Add("@callid");
            }
            else
            {
                if (txtDate.Text != "")
                {
                    sqlWHERE = sqlWHERE + " and calldt = convert (varchar(11), @calldt, 101)";
                    paramlist.Add("@calldt");
                }
                //if (txtFirstName.Text != "")
                //{
                //    sqlWHERE = sqlWHERE + " and firstname=@firstname";
                //    paramlist.Add("@firstname");
                //}
                //if (txtLastName.Text != "")
                //{
                //    sqlWHERE = sqlWHERE + " and lastname=@lastname";
                //    paramlist.Add("@lastname");
                //}
                if (ddlLocation.SelectedValue != "")
                {
                    sqlWHERE = sqlWHERE + " and location=@location";
                    paramlist.Add("@location");
                }
                if (ddlCallType.SelectedValue != "")
                {
                    sqlWHERE = sqlWHERE + " and calltypeid=@calltypeid";
                    paramlist.Add("@calltypeid");
                }
                if (ddlCallStatus.SelectedValue != "")
                {
                    sqlWHERE = sqlWHERE + " and statusid=@statusid";
                    paramlist.Add("@statusid");
                }
            }

            SqlParameter param;
            SqlConnection myConnection;
            myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
            myCommand = new SqlCommand(sqlSELECT + " " + sqlWHERE + " order by callid desc", myConnection);

            for(int i = 0; i < paramlist.Count(); i++)
            {
                if (paramlist[i] == "@companyid")
                {
                    param = new SqlParameter();
                    param.SqlDbType = SqlDbType.Int;
                    param.ParameterName = "@companyid";
                    param.Value = _companyid;
                    myCommand.Parameters.Add(param);
                }
                if (paramlist[i] == "@callid")
                {
                    param = new SqlParameter();
                    param.SqlDbType = SqlDbType.Int;
                    param.ParameterName = "@callid";
                    param.Value = int.Parse(txtCallID.Text);
                    myCommand.Parameters.Add(param);
                }
                if (paramlist[i] == "@calldt")
                {
                    param = new SqlParameter();
                    param.SqlDbType = SqlDbType.NVarChar;
                    param.ParameterName = "@calldt";
                    param.Value = txtDate.Text.Trim();
                    myCommand.Parameters.Add(param);
                }
                //if (paramlist[i] == "@firstname")
                //{
                //    param = new SqlParameter();
                //    param.SqlDbType = SqlDbType.NVarChar;
                //    param.ParameterName = "@firstname";
                //    param.Value = txtFirstName.Text.Trim();
                //    myCommand.Parameters.Add(param);
                //}
                //if (paramlist[i] == "@lastname")
                //{
                //    param = new SqlParameter();
                //    param.SqlDbType = SqlDbType.NVarChar;
                //    param.ParameterName = "@lastname";
                //    param.Value = txtLastName.Text.Trim();
                //    myCommand.Parameters.Add(param);
                //}
                if (paramlist[i] == "@location")
                {
                    param = new SqlParameter();
                    param.SqlDbType = SqlDbType.NVarChar;
                    param.ParameterName = "@location";
                    param.Value = ddlLocation.SelectedValue;
                    myCommand.Parameters.Add(param);
                }
                if (paramlist[i] == "@calltypeid")
                {
                    param = new SqlParameter();
                    param.SqlDbType = SqlDbType.Int;
                    param.ParameterName = "@calltypeid";
                    param.Value = ddlCallType.SelectedValue;
                    myCommand.Parameters.Add(param);
                }
                if (paramlist[i] == "@statusid")
                {
                    param = new SqlParameter();
                    param.SqlDbType = SqlDbType.Int;
                    param.ParameterName = "@statusid";
                    param.Value = ddlCallStatus.SelectedValue;
                    myCommand.Parameters.Add(param);
                }
            }
            

            SqlDataAdapter adapter = new SqlDataAdapter(myCommand);
            myConnection.Open();
            DataSet ds = new DataSet();
            adapter.Fill(ds);
            Session["DATATABLE"] = ds.Tables[0];
            gvCallList.DataSource = ds;
            gvCallList.DataBind();
            lblCallCount.Text = ds.Tables[0].Rows.Count.ToString();
            myConnection.Close();
        }

        protected void btnResetSearch_Click(object sender, EventArgs e)
        {
            Response.Redirect(Request.RawUrl);
        }

        private string ConvertSortDirectionToSql(SortDirection sortDirection)
        {
            string newSortDirection = String.Empty;

            switch (sortDirection)
            {
                case SortDirection.Ascending:
                    newSortDirection = "ASC";
                    break;

                case SortDirection.Descending:
                    newSortDirection = "DESC";
                    break;
            }
            return newSortDirection;
        }

        protected void gvCallList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvCallList.PageIndex = e.NewPageIndex;
            gvCallList.DataBind();
        }

        protected void gvCallList_Sorting(object sender, GridViewSortEventArgs e)
        {
            DataTable dataTable = Session["DATATABLE"] as DataTable;
            if (dataTable != null)
            {
                DataView dataView = new DataView(dataTable);
                dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);

                gvCallList.DataSource = dataView;
                gvCallList.DataBind();
            }
        }

    }

}
